package org.jeecg.modules.ncmeapis.network.entities.entity.dto;

import lombok.Data;
import org.apache.commons.lang.StringUtils;
import org.jeecg.modules.ncmeapis.network.entities.entity.GroupPathPerm;
import org.jeecg.modules.ncmeapis.network.entities.entity.UserPathPerm;
import org.springframework.beans.BeanUtils;

import java.util.Arrays;
import java.util.HashSet;

@Data
public class PathPermDTO {

    private Integer priority;
    private String pathId;
    private Integer depth;
    private String perms = "";

    public PathPermDTO(UserPathPerm userPathPerm) {
        BeanUtils.copyProperties(userPathPerm, this);
        this.setPriority(5);
    }

    public PathPermDTO(GroupPathPerm groupPathPerm) {
        BeanUtils.copyProperties(groupPathPerm, this);
        if (groupPathPerm.getGroupType().equals("1")) this.setPriority(4);  //部门
        else if (groupPathPerm.getGroupType().equals("2")) this.setPriority(4);  //角色
        else if (groupPathPerm.getGroupType().equals("3")) this.setPriority(4);  //租户
    }

    /**
     * 合并多个权限
     */
    public static PathPermDTO merge(PathPermDTO... pathPermDTOS) {
        //以第一个为基础
        PathPermDTO basePath = pathPermDTOS[0];
        for (int i = 1; i < pathPermDTOS.length; i++) {
            String basePerms = StringUtils.isNotEmpty(basePath.getPerms()) ? basePath.getPerms() + "," : "";
            PathPermDTO currentPath = pathPermDTOS[i];
            String currentPerms = StringUtils.isNotEmpty(currentPath.getPerms()) ? currentPath.getPerms() + "," : "";
            //合并
            basePath.setPerms(basePerms + currentPerms);
        }
        //去重
        basePath.setPerms(StringUtils.join(new HashSet<>(Arrays.asList(basePath.getPerms().split(","))), ","));
        return basePath;
    }

}
